package com.atguigu.daijia.customer.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerServiceImpl implements CustomerService {

    @Resource
    private CustomerInfoFeignClient customerInfoFeignClient;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 小程序授权登录
     * @param code
     * @return
     */
    @Override
    public String login(String code) {
        // 1 拿code 远程调用，得到用户id
        Result<Long> login = customerInfoFeignClient.login(code);
        // 2 判断是否获取成功
        if (login.getCode().equals(ResultCodeEnum.FAIL.getCode())) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        // 3 获取用户id
        Long userId = login.getData();
        if (userId == null) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //4 uuid 生成token
        String token = UUID.randomUUID().toString().replace("-", "");

        // 5 存入redis
        redisTemplate.opsForValue().set(
                RedisConstant.USER_LOGIN_KEY_PREFIX + token,
                userId.toString(),
                RedisConstant.USER_LOGIN_KEY_TIMEOUT,
                TimeUnit.SECONDS
        );
        return token;
    }

    /**
     * 获取登录用户信息, token 获取
     * @param token
     * @return
     */
    @Override
    public CustomerLoginVo getCustomerLoginInfo(String token) {
        // 拿到token去查询redis里的信息
        String customId = (String) redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);
        if (!StringUtils.hasText(customId)) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        return getCustomerLoginInfo(Long.parseLong(customId));
    }

    /**
     * 获取登录用户信息, 加入登录校验后的获取
     * @param userId
     * @return
     */
    @Override
    public CustomerLoginVo getCustomerLoginInfo(Long userId) {
        // 远程调用查询
        Result<CustomerLoginVo> customerLoginInfo = customerInfoFeignClient.getCustomerLoginInfo(userId);
        if (customerLoginInfo.getCode().equals(ResultCodeEnum.FAIL.getCode()) || customerLoginInfo.getData() == null) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        return customerLoginInfo.getData();
    }

    /**
     * 更新客户微信手机号码
     * @param form
     * @return
     */
    @Override
    public Boolean updateWxPhoneNumber(UpdateWxPhoneForm form) {
        Result<Boolean> result = customerInfoFeignClient.updateWxPhoneNumber(form);
//        if (result.getCode().equals(ResultCodeEnum.FAIL.getCode())) {
//            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
//        }
//        return result.getData();
        return true; // 因为只有企业微信号才能获取到电话号码
    }
}
